﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;

namespace DAL
{
    public class KhachHangDAL
    {
        //lay du lieu
        public List<KhachHangDTO> LayDanhSachKhachHangDAL()
        {
            ///TẠO KẾT NỐI
            string strcon = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database\SQL_QLSach.mdf;Integrated Security=True;User Instance=True";
            SqlConnection conn = new SqlConnection(strcon);
            ///NO KÊT NỐI
            conn.Open();
            ///TRUY VẤN VÀ THỰC THI
            string strQuyer = "SELECT * FROM DBKhachHang";
            SqlCommand com = new SqlCommand(strQuyer, conn);
            SqlDataReader dr = com.ExecuteReader();

            //dua ket qua vao danh sach thuc thi
            List<KhachHangDTO> ListKhachHang= new List<KhachHangDTO>();
            while (dr.Read())
            {
                KhachHangDTO khachhang = new KhachHangDTO();
                khachhang.MaKhachHang = dr.GetString(0);
                khachhang.TenKhachHang = dr.GetString(1);
                khachhang.DiaChi = dr.GetString(2);
                khachhang.SoDienThoai = dr.GetString(3);
                khachhang.Email = dr.GetString(4);
                //DAU VAO DANG SACH
                ListKhachHang.Add(khachhang);

            }
            ///DÓNG KẾT NỐI
            conn.Close();
            return ListKhachHang;
        }
    }
}
